package com.clouds.wms.cms;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.clouds.wms.bean.Cmsmb;
import com.clouds.wms.bean.Cmsmd;
import com.clouds.wms.bean.Cmsmk;
import com.clouds.wms.exception.SysException;
import com.clouds.wms.service.CmsmbService;
import com.clouds.wms.service.CmsmeService;
import com.clouds.wms.service.CmsmkService;
import com.clouds.wms.util.RequestHelper;

@Controller
@RequestMapping("/cmsmb")
public class CmsmbController {

	private Log log = LogFactory.getLog(getClass());
	
	@Resource(name="cmsmbService")
	private CmsmbService cmsmbService;

	@Resource(name="cmsmkService")
	private CmsmkService cmsmkService;

	@Resource(name="cmsmeService")
	private CmsmeService cmsmeService;
	

	/**
	 * 部门管理
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/cmsmbMain.xhtml")
	public String cmsmbMain(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		model.addAttribute("allCmsmeList", cmsmeService.getAllCmsmeList());
		return "cms/cmsmb";
	}

	/**
	 * 列出所有的部门
	 * @param request
	 * @param response
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/cmsmbList.xhtml")
	public String cmsmbList(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String mbaa = RequestHelper.getString(request, "mbaa");
		String mbab = RequestHelper.getString(request, "mbab");
		
		// 分页参数-start
		int count = 0;// 总记录数
		int pageNumber = RequestHelper.getInt(request, "page", 1);// 要跳转的页号
		int pageCount = 0;// 总页数
		int pageSize = RequestHelper.getInt(request, "rows", 10);// 一页的条数
		int offset = 0;// 起始记录数

		List<Cmsmb> list = null;
		count = cmsmbService.getCmsmbCount(mbaa, mbab);
		if (count != 0) {
			if (count % pageSize == 0) {
				pageCount = count / pageSize;
			} else {
				pageCount = count / pageSize + 1;
			}
			if (pageNumber > pageCount) {
				pageNumber = pageCount;
			}
			if (pageNumber < 1) {
				pageNumber = 1;
			}
			offset = (pageNumber - 1) * pageSize;
			list = cmsmbService.getCmsmbList(mbaa, mbab, offset, pageSize);
		} else {
			list = new ArrayList<Cmsmb>();
		}

		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			JSONObject datas = new JSONObject();
			// 设置总共有多少条记录
			datas.put("total", count);
			// 设置当前页的数据
			datas.put("rows", list);
			out.print(datas.toString());
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return null;
	}

	/**
	 * 添加部门
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/saveCmsmb.xhtml", method = RequestMethod.POST)
	public String saveCmsmb(HttpServletRequest request, HttpServletResponse response, Model model, Cmsmb cmsmb) throws SysException, IOException {
		String mbaa = cmsmb.getMbaa();
		String[] cmsmeMeaas = request.getParameterValues("meaaAdd");
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if(mbaa == null || "".equals(mbaa.trim())) {
			out.print("部门编号不能为空!");
			return null;
		}
		Cmsmb cmsmb1 = cmsmbService.getCmsmbByMbaa(mbaa);
		if(cmsmb1 != null && cmsmb1.getMbaa() != null && !"".equals(cmsmb1.getMbaa())) {
			out.print("部门编号已经存在!");
			return null;
		}
		//记录日志
		cmsmbService.saveCmsmb(cmsmb, cmsmeMeaas);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("增加了编号为[" + mbaa + "]的部门!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}

	/**
	 * 修改部门
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/updateCmsmb.xhtml", method = RequestMethod.POST)
	public String updateCmsmb(HttpServletRequest request, HttpServletResponse response, Model model, Cmsmb cmsmb) throws SysException, IOException {
		String mbaa = cmsmb.getMbaa();
		String[] cmsmeMeaas = request.getParameterValues("meaaUpdate");
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if(mbaa == null || "".equals(mbaa.trim())) {
			out.print("部门编号不能为空!");
			return null;
		}
		Cmsmb cmsmb1 = cmsmbService.getCmsmbByMbaa(mbaa);
		if(cmsmb1 != null && cmsmb1.getMbaa() != null && !"".equals(cmsmb1.getMbaa()) && !cmsmb1.getMbaa().equals(cmsmb.getMbaa())) {
			out.print("部门编号已经存在!");
			return null;
		}
		cmsmbService.updateCmsmb(cmsmb, cmsmeMeaas);
		// 记录日志
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("修改了编号为[" + mbaa + "]的部门!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}

	/**
	 * 删除部门
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/deleteCmsmb.xhtml", method = RequestMethod.POST)
	public String deleteCmsmb(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mbaa = RequestHelper.getString(request, "mbaa");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		cmsmbService.deleteCmsmbByMbaa(mbaa);
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		// 记录日志
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("删除了编号为[" + mbaa + "]的部门!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}

	/**
	 * 批量删除部门
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/deleteCmsmbBatch.xhtml", method = RequestMethod.POST)
	public String deleteCmsmbBatch(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mbaas = RequestHelper.getString(request, "mbaa");
		String[] mkaaStrs = mbaas.split(",");
		for(String mbaa : mkaaStrs) {
			cmsmbService.deleteCmsmbByMbaa(mbaa);
			Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
			// 记录日志
			Cmsmk cmsmk = new Cmsmk();
			cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
			cmsmk.setMkac(globalUmUser.getMdac());
			cmsmk.setMkad("删除了编号为[" + mbaa + "]的部门!");
			cmsmk.setMkae(new Date());
			cmsmk.setMkaf(request.getRemoteAddr());
			cmsmkService.saveCmsmk(cmsmk);
		}
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print("success");
		return null;
	}

}
